package com.example.jiaoan.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jiaoan.R;
import com.example.jiaoan.model.PlanModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class Plans extends RecyclerView.Adapter<Plans.holderView> {

    List<PlanModel> planModelList;

    PlanItemListener planItemListener;

    public Plans(List<PlanModel> planModelList, PlanItemListener planItemListener) {
        this.planModelList = planModelList;
        this.planItemListener = planItemListener;
    }


    @NonNull
    @NotNull
    @Override
    public holderView onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.plan_item,parent,false);
        return new holderView(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull holderView holder, int position) {
        holder.plan_title.setText(planModelList.get(position).getContent());
        holder.homework.setText(planModelList.get(position).getHomework());
        holder.plan_bar.setProgress(planModelList.get(position).getNum());
        holder.plan_bar.setIndeterminate(false);
        holder.planItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                planItemListener.OnPlanItemClick(planModelList.get(position).getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return this.planModelList.size();
    }

    public static class holderView extends RecyclerView.ViewHolder{

        TextView plan_title;
        TextView homework;
        ProgressBar plan_bar;
        LinearLayout planItem;

        public holderView(@NonNull @NotNull View itemView) {
            super(itemView);
            plan_title = itemView.findViewById(R.id.plan_title);
            homework = itemView.findViewById(R.id.plan_homework);
            plan_bar = itemView.findViewById(R.id.plan_bar);
            planItem = itemView.findViewWithTag("planItem");
        }
    }

    public interface PlanItemListener{
        void OnPlanItemClick(int id);
    }
}
